package org.java2me.mobileboxing;

import java.io.InputStream;

import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.game.GameCanvas;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;

/**
 * Created by IntelliJ IDEA. User: aterreno
 */
public class MobileBoxingCanvas extends GameCanvas implements Runnable {
	private static final Font FONT = Font.getFont(Font.FACE_PROPORTIONAL, Font.STYLE_BOLD, Font.SIZE_LARGE);
	private Status status = Status.IDLE();
	private final Display display;
	private final Graphics graphics;
	private Thread thread;
	private final long sleepTime = 1000;
	private CommandListener commandListener;

	public MobileBoxingCanvas(Display display, CommandListener commandListener) {
		super(false);
		this.display = display;
		this.graphics = getGraphics();
		setFullScreenMode(true);
		initCommands(commandListener);
	}

	private void initCommands(CommandListener commandListener) {		
		this.commandListener = commandListener;
		setCommandListener(this.commandListener);
		addCommand(MobileBoxingCommandListener.START);
		addCommand(MobileBoxingCommandListener.PAUSE);
		addCommand(MobileBoxingCommandListener.EXIT);
	}

	protected void showNotify() {
		thread = new Thread(this);
		thread.start();
	}

	protected void hideNotify() {
		thread = null;
	}

	private void drawStatus() {
		graphics.setColor(0);
		graphics.setFont(FONT);
		int height = getHeight() / 2 - graphics.getFont().getHeight();
		graphics.drawString(status.getTimeStatus(), getWidth() / 2, height, Graphics.TOP | Graphics.LEFT);
		graphics.drawString(status.description, getWidth() / 2, getHeight() / 2, Graphics.TOP | Graphics.LEFT);
	}

	private void clearScreen() {
		graphics.setColor(status.rgbColor);
		graphics.fillRect(0, 0, getWidth(), getHeight());
	}

	public void keyPressed(int keyCode) {		
		if (getGameAction(keyCode) == FIRE) {	
			if (status.isIdle())
				commandListener.commandAction(MobileBoxingCommandListener.START, this);
			else 
				commandListener.commandAction(MobileBoxingCommandListener.PAUSE, this);
		} 
	}

	protected void pointerPressed(int x, int y) {
		if (status.isIdle())
			commandListener.commandAction(MobileBoxingCommandListener.START, this);
		else 
			commandListener.commandAction(MobileBoxingCommandListener.PAUSE, this);
	}

	public void updateStatus(Status status) {
		this.status = status;
		display.vibrate(2000);
		display.flashBacklight(3000);
		InputStream in = getClass().getResourceAsStream("/clock.wav");
		try {
			Player p = Manager.createPlayer(in, "audio/x-wav");
			p.start();
		} catch (Exception ignoreandplaydefaultsound) {
			ignoreandplaydefaultsound.printStackTrace();
			AlertType.ALARM.playSound(display);
		}
	}

	public void run() {
		while (thread == Thread.currentThread()) {
			clearScreen();
			drawStatus();
			flushGraphics();
			try {
				Thread.sleep(sleepTime);
			} catch (InterruptedException ignore) {
			}
		}
	}
}
